package com.lablzh.gdxy.controller;

import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.assistant.Assistant;
import com.lablzh.gdxy.entity.assistant.AssistantApply;
import com.lablzh.gdxy.entity.assistant.StudentAssistant;
import com.lablzh.gdxy.entity.course.Course;
import com.lablzh.gdxy.entity.practice.Practice;
import com.lablzh.gdxy.entity.practice.PracticeApply;
import com.lablzh.gdxy.repository.assistant.AssistantApplyRepository;
import com.lablzh.gdxy.repository.assistant.AssistantRepository;
import com.lablzh.gdxy.repository.assistant.StudentAssistantRepository;
import com.lablzh.gdxy.service.AssistantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/assistant")
public class AssistantController {
    @Autowired
    private AssistantService assistantService;
    @Autowired
    private UserDetailsUtil userDetailsUtil;
    @Autowired
    private StudentAssistantRepository studentAssistantRepository;
@Autowired
private AssistantApplyRepository assistantApplyRepository;
    @Autowired
    private AssistantRepository assistantRepository;
    /**
     * 创建助教助研项目
     *
     * @param assistant
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @PostMapping("/team")
    public String createTeam(@RequestBody Assistant assistant) {

        return assistantService.createTeam(assistant);
    }

    /**
     * 助研助教删除已申请通过的学生
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @DeleteMapping("/deleteStudent")
    public String deleteStudent(@RequestParam("id") Integer id) {

        AssistantApply assistantApply = assistantApplyRepository.findById(id).orElse(null);

        Assistant assistant = assistantRepository.findById(assistantApply.getProjectID()).orElse(null);

        assistantRepository.updateRecvAndApplyedById(assistant.getId(), assistant.getRecv() - 1, assistant.getApplyed() - 1);

        studentAssistantRepository.deleteByProjectIDAndStudentUsername(assistant.getId(), assistantApply.getStudentUsername());
        assistantApplyRepository.deleteById(id);

        return "删除成功！";
    }

    /**
     * 修改助教助研项目
     *
     * @param assistant
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @PutMapping("/team")
    public String updateTeam(@RequestBody Assistant assistant) {
        return assistantService.updateTeam(assistant);
    }

    /**
     * 获取所有项目
     *
     * @return
     */

    @GetMapping("/team")
    public List<Assistant> getAllTeam() {
        return assistantService.getAllTeam();
    }
//    private String name;//；助教助研名称
//    private String college;//学院
//    private String major;//专业
//    private String category;//栏目类别
//    private String grade;//年级
//    private String department;//系别
//    private String teacher;//老师
    //是否
@PreAuthorize("hasAnyRole('admin', 'teacher','student')")
@PostMapping("/queryAssistant")
public Map getQueryAllAssistant(@RequestBody(required = false) Assistant assistant,
                              @RequestParam(name = "page", defaultValue = "1") Integer page) {
//        System.out.println("teacher:"+teacher);
    int nowPage=page-1;
    ExampleMatcher exampleMatcher = ExampleMatcher.matching()
            .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
            .withMatcher("college", ExampleMatcher.GenericPropertyMatchers.contains())
            .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
            .withMatcher("category", ExampleMatcher.GenericPropertyMatchers.contains())
            .withMatcher("teacher", ExampleMatcher.GenericPropertyMatchers.contains());


    Example<Assistant> example = Example.of(assistant, exampleMatcher);
    Pageable assistantPage = PageRequest.of(nowPage, 20, Sort.by(Sort.Direction.DESC, "id"));
    Page<Assistant> all = assistantRepository.findAll(example, assistantPage);
    Map<String, Object> data = new HashMap<>();
    data.put("totalPage", all.getTotalPages());
    data.put("total", all.getTotalElements());
    data.put("assistant", all.getContent());
    return data;
}
    /**
     * 根据id获取项目
     *
     * @param id
     * @return
     */

    @GetMapping("/oneTeam")
    public Assistant getOneTeam(@RequestParam("ID") Integer id) {
        return assistantService.getOneTeam(id);
    }

    /**
     * 根据id删除项目
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @DeleteMapping("/team")
    public String deleteTeam(@RequestParam("ID") Integer id) {
        return assistantService.deleteTeam(id);
    }

    /**
     * 学生申请助研项目
     *
     * @param id
     * @return
     */

    @PostMapping("/apply")
    public String apply(@RequestParam("ID") Integer id) {
        return assistantService.apply(id);
    }


    @DeleteMapping("/apply")
    public String deleteApply(@RequestParam("ID") Integer id) {

        return assistantService.undo(id);
    }

    /**
     * 教师查看申请表
     *
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @GetMapping("/applyTeacher")
    public List<AssistantApply> applyTeacher() {
        return assistantService.applyTeacher();
    }

    /**
     * 学生查看自己的申请
     *
     * @return
     */

    @GetMapping("/applyStudent")
    public List<AssistantApply> applyStudent() {
        return assistantService.applyStudent();
    }

    /**
     * 查看某课程申请情况
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @GetMapping("/applyTeam")
    public List<AssistantApply> applyTeam(@RequestParam("ID") Integer id) {
        return assistantService.applyTeam(id);
    }

    /**
     * 审核通过
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @PutMapping("/confirm")
    public String confirm(@RequestParam("ID") Integer id) {
        return assistantService.confirm(id);
    }

    /**
     * 退回重选
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @PutMapping("/refuse")
    public String refuse(@RequestParam("id") Integer id) {
        return assistantService.refuse(id);
    }

    /**
     * 条件查询助教
     *
     * @param studentAssistant
     * @return
     */
    @PostMapping("/queryStudentAssistant")
    public List<StudentAssistant> export(@RequestBody StudentAssistant studentAssistant) {
        return assistantService.queryPerformance(studentAssistant);
    }

    /**
     * 导出学生选题情况统计报表
     *
     * @param type
     * @param college
     * @param major
     * @param grade
     * @param category
     * @param request
     * @param response
     * @return
     * @throws FileNotFoundException
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @GetMapping("/studentChoose")
    public void getStudentChoose(@RequestParam("type") String type,
                                 @RequestParam("college") String college,
                                 @RequestParam("major") String major,
                                 @RequestParam("grade") String grade,
                                 @RequestParam("category") String category,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws FileNotFoundException {
        assistantService.getStudentChoose(type, college, major, grade, category, request, response);
    }

    /**
     * 导出题目互选统计表
     *
     * @param college
     * @param grade
     * @param category
     * @param request
     * @param response
     * @return
     * @throws FileNotFoundException
     */
    @PreAuthorize("hasAnyRole('teacher','admin')")
    @GetMapping("/assistantChoose")
    public void getAssistantChoose(@RequestParam("college") String college,
                                   @RequestParam("grade") String grade,
                                   @RequestParam("category") String category,
                                   HttpServletRequest request,
                                   HttpServletResponse response
    ) throws FileNotFoundException {
        assistantService.getAssistantChoose(college, grade, category, request, response);
    }

    @PreAuthorize("hasAnyRole('teacher','admin')")
    @GetMapping("/assistantArrange")
    public void getAssistantArrange(@RequestParam("college") String college,
                                    @RequestParam("major") String major,
                                    @RequestParam("grade") String grade,
                                    @RequestParam("category") String category,
                                    @RequestParam("sortMethod") String sortMethod,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws FileNotFoundException {

        assistantService.getAssistantArrange(college, major, grade, category, sortMethod, request, response);
    }

    @PreAuthorize("hasAnyRole('teacher','admin')")
    @GetMapping("/assistantOffer")
    public void getAssistantOffer(@RequestParam("college") String college,
                                  @RequestParam("major") String major,
                                  @RequestParam("grade") String grade,
                                  @RequestParam("category") String category,
                                  @RequestParam("sortMethod") String sortMethod,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws FileNotFoundException {
        assistantService.getAssistantOffer(college, major, grade, category, sortMethod, request, response);
    }

}
